package cn.jxau.qt.core.servlet;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.jxau.qt.core.utils.WebUtils;

public abstract class BaseServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 跳转资源
		Object returnValue = null;
		// 获取操作类型 ， 约定：操作类型的值必须对应servlet中的方法名称
		String methodName = request.getParameter("method");
		try {
			// 1.获取当前运行类的字节码
			Class<? extends BaseServlet> clazz = this.getClass();
			// 2.获取当前执行的方法的mehod类型
			Method method = clazz.getDeclaredMethod(methodName,
					HttpServletRequest.class, HttpServletResponse.class);
			// 3.执行方法
			returnValue = method.invoke(this, request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		WebUtils.goTo(request, response, returnValue);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);

	}

}
